package spring.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;

import spring.pojo.Employee;

/**
 * @author CJunGod 1038804380@qq.com
 *
 */
@RepositoryDefinition(domainClass = Employee.class,idClass = Integer.class)
public interface EmployeeService{

	Employee findByName(String name);
	
	//where name like ?% and age <?
	List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age);
	
	//where name like %? and age <?
	List<Employee> findByNameEndingWithAndAgeLessThan(String name, Integer age);
	
	//where name in (?,?....) or age<?
	List<Employee> findByNameInOrAgeLessThan(List<String> names, Integer age);
	
	//where name in (?,?....) and age<?
	List<Employee> findByNameInAndAgeLessThan(List<String> names, Integer age);
	
	@Query("select e from Employee e where id=(select max(id) from Employee t1) ")
	Employee getEmployeeByMaxId();
	
	@Query("select e from Employee e where e.name=?1 and e.age=?2")
	List<Employee> queryParams(String name, Integer age);
	
	@Query("select e from Employee e where e.name=:name and e.age=:age")
	List<Employee> queryParams2(@Param("name")String name, @Param("age")Integer age);
	
	@Query("select e from Employee e where e.name like %?1%")
	List<Employee> queryLike(String name);
	
	@Query("select e from Employee e where e.name like %:name%")
	List<Employee> queryLike2(@Param("name")String name);
	
	@Query(nativeQuery = true, value = "select count(1) from employee")
	long getCount();
	
	@Modifying
	@Query("update Employee e set e.age = :age where e.id = :id")
	int update(@Param("id")Integer id, @Param("age")Integer age);
}
